// server1.js 代理服务器(http://localhost:3000)
const http = require('http')

const data1 = { title: 'get..', password: '123' }
const data2 = { title: 'post..', password: '456' }

// 第一步：接受客户端请求
const server = http.createServer((request, response) => {
    // 代理服务器，直接和浏览器直接交互，需要设置CORS 的首部字段
    response.writeHead(200, {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE',
        'Access-Control-Allow-Headers': 'Content-Type,Token'
    })

    if (request.url === '/get') {
        response.end(JSON.stringify(data1))
    }
    if (request.url === '/post') {
        response.end(JSON.stringify(data2))
    }
})
server.listen(3000, () => {
    console.log('The proxyServer is running at http://localhost:3000')
})

